#!/usr/bin/env python3
"""
镜像评估命令行工具
"""

import argparse
import logging
import sys
from pathlib import Path

# 添加模块路径
sys.path.insert(0, str(Path(__file__).parent.parent))

from gitsubmodule.core import GitSubmoduleManager
from gitsubmodule.evaluator import MirrorEvaluator
from gitsubmodule.config import ConfigManager

def setup_logging(verbose: bool = False):
    """设置日志"""
    level = logging.DEBUG if verbose else logging.INFO
    logging.basicConfig(
        level=level,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    )

def main():
    parser = argparse.ArgumentParser(description='Git 子模块镜像评估工具')
    parser.add_argument('project', help='项目名称')
    parser.add_argument('original_url', help='原始仓库URL')
    parser.add_argument('mirror_url', help='镜像仓库URL')
    parser.add_argument('--repo-path', default='.', help='Git 仓库路径')
    parser.add_argument('-v', '--verbose', action='store_true', help='详细输出')
    
    args = parser.parse_args()
    setup_logging(args.verbose)
    
    try:
        # 初始化管理器
        git_manager = GitSubmoduleManager(args.repo_path)
        evaluator = MirrorEvaluator(git_manager)
        
        # 评估镜像
        result = evaluator.evaluate_mirror(args.original_url, args.mirror_url)
        
        # 输出结果
        print("\n" + "="*50)
        print(f"镜像评估结果: {args.project}")
        print("="*50)
        print(f"原始仓库: {result['original_url']}")
        print(f"镜像仓库: {result['mirror_url']}")
        print(f"可用性: {'✅ 可用' if result['availability'] else '❌ 不可用'}")
        print(f"新鲜度: {result['freshness_score']}/30")
        print(f"完整性: {result['completeness_score']}/30")
        print(f"综合得分: {result['total_score']}/100")
        print(f"推荐建议: {result['recommendation']}")
        print("="*50)
        
        return 0 if result['total_score'] >= 60 else 1
        
    except Exception as e:
        logging.error(f"评估失败: {e}")
        return 1

if __name__ == '__main__':
    sys.exit(main())

